package system;

import android.view.KeyEvent;

public class KeyBoard implements Constants{
	private static KeyEvent s_preKeyPressed;
	private static int GetDeviceKey(int keyCode)
    {  	
        switch (keyCode)
        {       
			case KeyEvent.KEYCODE_DPAD_UP:
				return Constants.DK_PAD_UP;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				return Constants.DK_PAD_DOWN;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				return DK_PAD_LEFT;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				return DK_PAD_RIGHT;
			case KeyEvent.KEYCODE_DPAD_CENTER:
				return DK_PAD_MIDDLE;
			case KEY_SOFT_LEFT:
				return DK_SOFT_LEFT;
			case KEY_SOFT_RIGHT:
				return DK_SOFT_RIGHT;
			case KeyEvent.KEYCODE_0:
				return DK_NUM0;
			case KeyEvent.KEYCODE_1:
				return DK_NUM1;
			case KeyEvent.KEYCODE_2:
				return DK_NUM2;
			case KeyEvent.KEYCODE_3:
				return DK_NUM3;
			case KeyEvent.KEYCODE_4:
				return DK_NUM4;
			case KeyEvent.KEYCODE_5:
				return DK_NUM5;
			case KeyEvent.KEYCODE_6:
				return DK_NUM6;
			case KeyEvent.KEYCODE_7:
				return DK_NUM7;
			case KeyEvent.KEYCODE_8:
				return DK_NUM3;
			case KeyEvent.KEYCODE_9:
				return DK_NUM3;
			case KeyEvent.KEYCODE_A:
			case KeyEvent.KEYCODE_B:
			case KeyEvent.KEYCODE_C:
			case KeyEvent.KEYCODE_D:
			case KeyEvent.KEYCODE_E:
			case KeyEvent.KEYCODE_F:
			case KeyEvent.KEYCODE_G:
			case KeyEvent.KEYCODE_H:
			case KeyEvent.KEYCODE_I:
			case KeyEvent.KEYCODE_J:
			case KeyEvent.KEYCODE_K:
			case KeyEvent.KEYCODE_L:
			case KeyEvent.KEYCODE_M:
			case KeyEvent.KEYCODE_N:
			case KeyEvent.KEYCODE_O:
			case KeyEvent.KEYCODE_P:
			case KeyEvent.KEYCODE_Q:
			case KeyEvent.KEYCODE_R:
			case KeyEvent.KEYCODE_S:
			case KeyEvent.KEYCODE_T:
			case KeyEvent.KEYCODE_U:
			case KeyEvent.KEYCODE_V:
			case KeyEvent.KEYCODE_W:
			case KeyEvent.KEYCODE_X:
			case KeyEvent.KEYCODE_Y:
			case KeyEvent.KEYCODE_Z:
				return keyCode;
			default:
				return 0;
        }
    }

    private static int s_keyPressed = 0;
    private static int s_keyPressedInStore;
    private static int s_keyCurrent;
    private static int s_keyHold;
    private static int s_keyReleased;
    private static int s_keyPrev;
    private static boolean s_IsNeedToUpdate = false;
    private static long s_lLastTime = 0;
    private final static long s_lDelayTime = 2;
    
	public static void onKeyDown(int keyCode, KeyEvent event)
    {
		s_lLastTime = System.currentTimeMillis();
        s_keyPressed |= GetDeviceKey(keyCode);
        s_keyHold |= s_keyPressed;
        s_keyCurrent |= s_keyPressed;
        s_keyReleased = 0;
	}
	
	public static void onKeyUp(int keyCode, KeyEvent event)
    {
		s_lLastTime = System.currentTimeMillis();
		s_keyReleased	|= GetDeviceKey(keyCode);
		s_keyPrev 		|= s_keyReleased;
		s_IsNeedToUpdate = true;
//		s_keyCurrent	&= ~s_keyReleased;
//		s_keyHold		&= ~s_keyReleased;
//		s_keyPressed	&= ~s_keyReleased;
	}	
	
	public static void Update (){
		if (!s_IsNeedToUpdate)
			return;
		
		s_IsNeedToUpdate = false;
		s_keyCurrent	&= ~s_keyPrev;
		s_keyHold		&= ~s_keyPrev;
		s_keyPressed	&= ~s_keyPrev;
		if (System.currentTimeMillis() - s_lLastTime >= s_lDelayTime)
			s_keyReleased = 0;
	}
	
    public static void StoreAnyKeyPressed()
    {
        if (s_keyPressed != 0)
        {
            s_keyPressedInStore = s_keyPressed;
        }
    }

    public static boolean IsKeyPressedInStore(int gameKey)
    {
        return (s_keyPressedInStore & gameKey) != 0;
    }
    public static void ClearKeyPressedInStore()
    {
        s_keyPressedInStore = 0;
    }
    public static boolean IsKeyHold(int gameKey)
    {
        return (s_keyHold & gameKey) != 0;
    }

    public static boolean IsKeyPressed(int gameKey)
    {
        return (s_keyPressed & gameKey) == gameKey;
    }

    public static boolean IsKeyReleased(int gameKey)
    {
        return (s_keyReleased & gameKey) == gameKey;
    }

    public static boolean IsAnyKeyHold()
    {
        return (s_keyHold & 0x1FFFF) != 0;
    }

    public static boolean IsAnyKeyPressed()
    {
        return s_keyPressed != 0;
    }

    public static void ClearKey()
    {
    	s_keyPrev = s_keyCurrent = s_keyHold = s_keyPressed = s_keyReleased = s_keyPressedInStore = 0;
    }
}
